function stateUpdated() {
  if(!wave.getState().get('count')) {
    div.innerHTML = "Count: 0."
  } else {
    div.innerHTML = "Count: " + wave.getState().get('count') + '.';
  }
  gadgets.window.adjustWidth();
  gadgets.window.adjustHeight();
}

function init() {
  if (wave && wave.isInWaveContainer()) {
    wave.setStateCallback(stateUpdated);
  }
}

function buttonClicked() {
  var value = parseInt(wave.getState().get('count', '0'));
  wave.getState().submitDelta({'count': value + 1});
  gadgets.window.adjustWidth();
  gadgets.window.adjustHeight();
}

function resetCounter(){
  wave.getState().submitDelta({'count': '0'});
  gadgets.window.adjustWidth();
  gadgets.window.adjustHeight();
}

document.write('<table id="content_block" border="0"><tr><td nowrap><span id="content_div">Count: None.</span>');
document.write('<input type=button value="Click Me!" id="butCount" onClick="buttonClicked()">');
document.write('<input type=button value="Reset" id="butReset" onClick="resetCounter()"></td></tr></table>');
var div = document.getElementById('content_div');
var tab = document.getElementById('content_block');

gadgets.util.registerOnLoadHandler(init);